<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs2.model.*, bbs2.dao.*, bbs2.service.*, bbs2.util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="../_checkUser.jsp" %>
<%
    RequestParameter param = new RequestParameter(request);
    int bid = param.getInt("bid", 0);
    int aid = param.getInt("aid", 0);
    String cmd = param.getString("cmd", "");
    
    int userId = UserService.getCurrentUser(session).getId();
    Article article = ArticleDAO.selectById(aid, UserService.getCurrentUser(session).getId());
    BoardAccessRule rule = BoardAccessRuleDAO.selectByUserId(bid, userId);
    
    String urlList = "list.jsp?" + request.getQueryString().replaceAll("&?aid=[0-9]+","").replaceAll("&?cmd=[a-z]+", "");
    
    if (cmd.equals("delete")) {
        if (rule.canDeleteArticle() || (rule.canDeleteMyArticle() && article.getUserId() == userId)) {
            ArticleDAO.deleteById(aid);
            response.sendRedirect(urlList);
        } else
            response.sendRedirect("/bbs2/guest/error403.jsp");
    } else if (cmd.equals("notice")) {
        if (rule.canManageBoard()) {
            article.setNotice(!article.isNotice());
            ArticleDAO.update(article);
            response.sendRedirect(urlList);
        } else
            response.sendRedirect("/bbs2/guest/error403.jsp");
    }
%>